package com.gexiaobao.pigeon.ui.breeding_pigeon

import android.annotation.SuppressLint
import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.view.isGone
import androidx.core.view.isInvisible
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.eventViewModel
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.showMessage
import com.gexiaobao.pigeon.app.model.bean.BloodBookDetailResponse
import com.gexiaobao.pigeon.app.model.bean.EditBloodInfoBean
import com.gexiaobao.pigeon.app.model.param.MakePedigreeBookParam
import com.gexiaobao.pigeon.app.rxui.tablayout.listener.GlideEngine
import com.gexiaobao.pigeon.app.util.ImageLoaderManager
import com.gexiaobao.pigeon.app.util.PictureUtil
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.ActivityMakeBloodBinding
import com.gexiaobao.pigeon.ui.adapter.AdapterFourGeneration
import com.gexiaobao.pigeon.ui.adapter.AdapterOneGeneration
import com.gexiaobao.pigeon.ui.adapter.AdapterThreeGeneration
import com.gexiaobao.pigeon.ui.adapter.AdapterTwoGeneration
import com.gexiaobao.pigeon.ui.dialog.RxDialogDefault
import com.gexiaobao.pigeon.ui.dialog.RxDialogSingle
import com.gexiaobao.pigeon.viewmodel.BreedingPigeonViewModel
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.gson.Gson
import com.luck.picture.lib.basic.PictureSelectionModel
import com.luck.picture.lib.basic.PictureSelector
import com.luck.picture.lib.config.SelectMimeType
import com.luck.picture.lib.config.SelectModeConfig
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.interfaces.OnResultCallbackListener
import com.luck.picture.lib.language.LanguageConfig
import com.luck.picture.lib.style.PictureSelectorStyle
import com.zs.easy.imgcompress.EasyImgCompress
import com.zs.easy.imgcompress.listener.OnCompressSinglePicListener
import me.hgj.jetpackmvvm.ext.parseState
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat
import me.hgj.jetpackmvvm.util.LogUtils
import me.hgj.jetpackmvvm.util.startActivity
import me.hgj.jetpackmvvm.util.startActivityForResult
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import java.io.File

/**
 * @Author      : hxw
 * @Date        : 2024/9/13 9:28
 * @Describe    : 制作血统书（代代布局）
 */
class ActivityMakeBlood : BaseActivity<BreedingPigeonViewModel, ActivityMakeBloodBinding>() {

    private var notEditable = 0
    private var editBloodBean: EditBloodInfoBean? = null
    private var intentType: String = ""
    private var mId: Int = 0
    private var bloodId: Int = 0//种鸽进入时血统书的id
    private var pathUrl: String = ""
    private var imagePath: String = ""
    private var coverImageUrl: String = ""
    private var saveType: String = ""
    private var imageId = 0

    private val mAdapter1: AdapterOneGeneration by lazy { AdapterOneGeneration(arrayListOf()) }
    private val mList1: ArrayList<EditBloodInfoBean> = arrayListOf()

    private val mAdapter2: AdapterTwoGeneration by lazy { AdapterTwoGeneration() }
    private val mList2: ArrayList<EditBloodInfoBean> = arrayListOf()

    private val mAdapter3: AdapterThreeGeneration by lazy { AdapterThreeGeneration() }
    private val mList3: ArrayList<EditBloodInfoBean> = arrayListOf()

    private val mAdapter4: AdapterFourGeneration by lazy { AdapterFourGeneration() }
    private val mList4: ArrayList<EditBloodInfoBean> = arrayListOf()

    private val mParmaList: ArrayList<EditBloodInfoBean> = arrayListOf()
    private val mRequestList: ArrayList<EditBloodInfoBean> = arrayListOf()
    private lateinit var mParam: MakePedigreeBookParam
    private lateinit var mInfoDetail: BloodBookDetailResponse

    @SuppressLint("NotifyDataSetChanged")
    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setTranslucentForImageView(this, 0, null)
        mDatabind.includeBar.tvTitle.text = getString(R.string.make_blood_book)
        mDatabind.includeBar.tvRightMenu.isVisible = true
        mDatabind.includeBar.tvRightMenu.text = getString(R.string.download)

        intentType = intent.getStringExtra("type").toString()
        mId = intent.getIntExtra("pigeonId", 0)
        if (intentType == "edit") {//编辑
            showLoading(getString(R.string.loading))
            mViewModel.getBloodBookInfo(mId.toString())
        }
        if (intentType == "blood") {//种鸽进入
            editBloodBean = intent.getParcelableExtra("editBloodInfo")
            imagePath = intent.getStringExtra("imagePath").toString()
            coverImageUrl = intent.getStringExtra("imageUrl").toString()
            /** 获取血统书详情 */
            showLoading(getString(R.string.loading))
            mViewModel.getBloodBookInfoByPigeonId(mId.toString())
            mAdapter1.canClick(1, false)
        }
        if (intentType == "grade") {
            editBloodBean = intent.getParcelableExtra("editBloodInfo")
            val memberId = intent.getIntExtra("memberId", 0)
            val ringId = intent.getStringExtra("ringId")
            val map = mapOf(
                "memberId" to memberId,
                "ringId" to ringId
            )
            val body: RequestBody = Gson().toJson(map).toRequestBody("application/json".toMediaTypeOrNull())
            showLoading(getString(R.string.loading))
            mViewModel.getBloodBook(body)
        }

        mDatabind.recycleView1.init(LinearLayoutManager(this), mAdapter1).addItemDecoration(
            SpaceItemDecoration(0, Util.dp2px(this, 8f))
        )

        mDatabind.recycleView2.init(LinearLayoutManager(this), mAdapter2).addItemDecoration(
            SpaceItemDecoration(0, Util.dp2px(this, 4f))
        )

        mDatabind.recycleView3.init(LinearLayoutManager(this), mAdapter3).addItemDecoration(
            SpaceItemDecoration(0, Util.dp2px(this, 4f))
        )

        mDatabind.recycleView4.init(LinearLayoutManager(this), mAdapter4).addItemDecoration(
            SpaceItemDecoration(0, 0)
        )

        for (i in 0..2) {
            mList1.add(EditBloodInfoBean())
        }
        mAdapter1.setList(mList1)
        mAdapter1.notifyDataSetChanged()

        for (i in 0..3) {
            mList2.add(EditBloodInfoBean())
        }
        mAdapter2.setList(mList2)
        mAdapter2.notifyDataSetChanged()

        for (i in 0..7) {
            mList3.add(EditBloodInfoBean())
        }
        mAdapter3.setList(mList3)
        mAdapter3.notifyDataSetChanged()

        for (i in 0..15) {
            mList4.add(EditBloodInfoBean())
        }
        mAdapter4.setList(mList4)
        mAdapter4.notifyDataSetChanged()

        initListener()
    }

    override fun createObserver() {
        super.createObserver()
        /** 获取血统书详情 */
        mViewModel.getBloodBookInfoResult.observe(this) { resultState ->
            parseState(resultState, {
                echoData(it)
            }, {
                mAdapter1.updateItem(1, editBloodBean)
                setAdapterClickAble()
                RxToast.warning(it.msg)
            })
        }
        /** 修改血统书 */
        mViewModel.editBloodBookResult.observe(this) {
            eventViewModel.toRefreshData.value = true
            if (saveType == "save") {
                finish()
            } else {
                showLoading(getString(R.string.Acquiring))
                mViewModel.getBloodBookInfo(bloodId.toString())
            }
        }
        /** 新增血统书 */
        mViewModel.addBloodBookResult.observe(this) {
            val mId = it.id
            eventViewModel.toRefreshDataInt.value = mId
            if (saveType == "save") {//save-普通保存血统书，download-保存后下载血统书
                finish()
            } else {
                if (mId != 0) {
                    showLoading(getString(R.string.Acquiring))
                    mViewModel.getBloodBookInfo(it.id.toString())
                } else {
                    LogUtils.warnInfo("血统书id获取失败")
                }
            }
        }
        /** 上传图片(单张) */
        mViewModel.upLoadBookImageResult.observe(this) {
            imageId = it.id
            imagePath = it.path
            mDatabind.ivDeleteCoverImage.isVisible = true
            mDatabind.ivImageCover.setImageURI(Uri.parse("file://$pathUrl"))
        }
    }

    private fun echoData(it: BloodBookDetailResponse?) {
        if (it != null) {
            bloodId = it.id
            mInfoDetail = it
            mRequestList.clear()
            mRequestList.addAll(it.list)
            if (bloodId != 0) imagePath = it.pgImgPath
            val url = if (bloodId == 0) {
                coverImageUrl
            } else {
                it.pgImg
            }
            if (url.isNotEmpty()) ImageLoaderManager.loadRoundImage(this, url, mDatabind.ivImageCover, 8)
            mDatabind.ivDeleteCoverImage.isVisible = url.isNotEmpty()
            if (it.list.isNotEmpty()) {
                it.list.forEachIndexed { index, editBloodInfoBean ->
                    when (editBloodInfoBean.levelInfo) {
                        "1" -> mAdapter1.updateItem(1, editBloodInfoBean)//主鸽
                        "1-1" -> mAdapter1.updateItem(0, it.list[index])//父亲
                        "1-2" -> mAdapter1.updateItem(2, it.list[index])//母亲
                        "1-1-1" -> mAdapter2.updateItem(0, it.list[index])//祖父
                        "1-1-2" -> mAdapter2.updateItem(1, it.list[index])//祖母
                        "1-2-1" -> mAdapter2.updateItem(2, it.list[index])//外祖父
                        "1-2-2" -> mAdapter2.updateItem(3, it.list[index])//外祖母
                        "1-1-1-1" -> mAdapter3.updateItem(0, it.list[index])//曾祖父
                        "1-1-1-2" -> mAdapter3.updateItem(1, it.list[index])//曾祖母
                        "1-1-2-1" -> mAdapter3.updateItem(2, it.list[index])//曾外祖父
                        "1-1-2-2" -> mAdapter3.updateItem(3, it.list[index])//曾外祖母
                        "1-2-1-1" -> mAdapter3.updateItem(4, it.list[index])//外曾祖父
                        "1-2-1-2" -> mAdapter3.updateItem(5, it.list[index])//外曾祖母
                        "1-2-2-1" -> mAdapter3.updateItem(6, it.list[index])//外曾外祖父
                        "1-2-2-2" -> mAdapter3.updateItem(7, it.list[index])//外曾外祖母
                        "1-1-1-1-1" -> mAdapter4.updateItem(0, it.list[index])//高祖父
                        "1-1-1-1-2" -> mAdapter4.updateItem(1, it.list[index])//高祖母
                        "1-1-1-2-1" -> mAdapter4.updateItem(2, it.list[index])//高外祖父
                        "1-1-1-2-2" -> mAdapter4.updateItem(3, it.list[index])//高外祖母
                        "1-1-2-1-1" -> mAdapter4.updateItem(4, it.list[index])//曾外曾祖父
                        "1-1-2-1-2" -> mAdapter4.updateItem(5, it.list[index])//曾外曾祖母
                        "1-1-2-2-1" -> mAdapter4.updateItem(6, it.list[index])//曾外曾外祖父
                        "1-1-2-2-2" -> mAdapter4.updateItem(7, it.list[index])//曾外曾外祖母
                        "1-2-1-1-1" -> mAdapter4.updateItem(8, it.list[index])//外高祖父
                        "1-2-1-1-2" -> mAdapter4.updateItem(9, it.list[index])//外高祖母
                        "1-2-1-2-1" -> mAdapter4.updateItem(10, it.list[index])//外高外祖父
                        "1-2-1-2-2" -> mAdapter4.updateItem(11, it.list[index])//外高外祖母
                        "1-2-2-1-1" -> mAdapter4.updateItem(12, it.list[index])//外曾外曾祖父
                        "1-2-2-1-2" -> mAdapter4.updateItem(13, it.list[index])//外曾外曾祖母
                        "1-2-2-2-1" -> mAdapter4.updateItem(14, it.list[index])//外曾外曾外祖父
                        "1-2-2-2-2" -> mAdapter4.updateItem(15, it.list[index])//外曾外曾外祖母
                    }
                }
            } else {
                mAdapter1.updateItem(1, editBloodBean)
            }
            if (saveType == "download" && ::mInfoDetail.isInitialized) {
                startActivity<ActivityPedigreeStyle>("dataBean" to mInfoDetail, "id" to mId)
            } else {
                LogUtils.warnInfo("数据初始化失败")
            }
            setAdapterClickAble()
        }
    }

    private fun setAdapterClickAble() {
        if (intentType == "grade") {
            mAdapter1.setOnItemChildClickListener(null)
            mAdapter2.setOnItemChildClickListener(null)
            mAdapter3.setOnItemChildClickListener(null)
            mAdapter4.setOnItemChildClickListener(null)
            mDatabind.conCoverImage.isClickable = false
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(
            mDatabind.includeBar.ivBack,
            mDatabind.includeBar.tvRightMenu,
            mDatabind.conCoverImage,
            mDatabind.ivDeleteCoverImage,
            mDatabind.btnSaveBloodBook
        ) {
            when (it) {
                mDatabind.btnSaveBloodBook -> savePedigreeBook("save")
                mDatabind.includeBar.tvRightMenu -> saveBloodDataAndBloodImage()
                mDatabind.includeBar.ivBack -> whetherToSaveData()
                mDatabind.ivDeleteCoverImage -> cleanCoverImage()
                mDatabind.conCoverImage -> selectCoverPic()
            }
        }
    }

    /** 保存血统书数据并下载图片 */
    private fun saveBloodDataAndBloodImage() {
        if (mAdapter1.data[1].ringId.isEmpty()) {
            showSingleDialog()
            return
        }
        if (!isSaveOrNotBloodBook()) {
            showDownLoadBloodBookDialog()
            return
        }
        savePedigreeBook("download")
    }

    private fun showDownLoadBloodBookDialog() {
        val dialog = RxDialogDefault(this)
        dialog.setTitle(getString(R.string.save_blood_book_and_download))
        dialog.setSureListener {
            savePedigreeBook("download")
            dialog.dismiss()
        }
        dialog.setFullScreenWidth()
        dialog.show()
    }

    private fun isSaveOrNotBloodBook(): Boolean {
        saveDataToParamList()
        if (mAdapter1.data[0].ringId.isNotEmpty()) {
            val index1 = 0
            val index2 = 1
            if (index1 in mParmaList.indices && index2 in mParmaList.indices) {
                // 互换元素
                val temp = mParmaList[index1]
                mParmaList[index1] = mParmaList[index2]
                mParmaList[index2] = temp
            } else {
                LogUtils.warnInfo("索引无效")
            }
        }
        return mParmaList == mRequestList
    }

    private fun showSingleDialog() {
        val dialog = RxDialogSingle(this)
        dialog.setTitle(getString(R.string.can_not_download_blood_book))
        dialog.setSureListener {
            dialog.dismiss()
        }
        dialog.setFullScreenWidth()
        dialog.show()
    }

    private fun cleanCoverImage() {
        imagePath = ""
        mDatabind.ivImageCover.setImageBitmap(null)
        mDatabind.ivDeleteCoverImage.isVisible = false
    }

    private fun whetherToSaveData() {
        saveDataToParamList()
        if (mAdapter1.data[0].ringId.isNotEmpty()) {
            val index1 = 0
            val index2 = 1
            if (index1 in mParmaList.indices && index2 in mParmaList.indices) {
                // 互换元素
                val temp = mParmaList[index1]
                mParmaList[index1] = mParmaList[index2]
                mParmaList[index2] = temp
            } else {
                LogUtils.warnInfo("索引无效")
            }
        }
        if (mParmaList == mRequestList) {
            finish()
        } else {
            val dialog = RxDialogDefault(this)
            dialog.setTitle(getString(R.string.not_save_blood_book))
            dialog.setRightBtnContent(getString(R.string.save))
            dialog.setLeftBtnContent(getString(R.string.un_save))
            dialog.setCancelListener {
                dialog.dismiss()
                finish()
            }
            dialog.setSureListener {
                savePedigreeBook("save")
                dialog.dismiss()
            }
            dialog.setFullScreenWidth()
            dialog.show()
        }
    }

    private fun saveDataToParamList() {
        mParmaList.clear()
        if (mAdapter1.data.isNotEmpty()) {
            mAdapter1.data.forEachIndexed { index, data ->
                if (data.ringId.isNotEmpty()) {
                    mParmaList.add(mAdapter1.data[index])
                }
            }
        }
        if (mAdapter2.data.isNotEmpty()) {
            mAdapter2.data.forEachIndexed { index, data ->
                if (data.ringId.isNotEmpty()) {
                    mParmaList.add(mAdapter2.data[index])
                }
            }
        }
        if (mAdapter3.data.isNotEmpty()) {
            mAdapter3.data.forEachIndexed { index, data ->
                if (data.ringId.isNotEmpty()) {
                    mParmaList.add(mAdapter3.data[index])
                }
            }
        }
        if (mAdapter4.data.isNotEmpty()) {
            mAdapter4.data.forEachIndexed { index, data ->
                if (data.ringId.isNotEmpty()) {
                    mParmaList.add(mAdapter4.data[index])
                }
            }
        }
    }

    private fun savePedigreeBook(type: String) {
        val dialog = RxDialogDefault(this)
        dialog.setContent("是否同时保存血统书图片?")
        dialog.setLeftBtnContent("仅保存数据")
        dialog.setRightBtnContent("确定")
        dialog.setSureListener {
            dialog.dismiss()
            if (mAdapter1.data[1].ringId.isEmpty()) {
                showSingleDialog()
                return@setSureListener
            }
            requestBloodData("download")
        }
        dialog.setCancelListener {
            dialog.dismiss()
            requestBloodData(type)
        }
        dialog.setFullScreenWidth()
        dialog.show()
    }

    private fun requestBloodData(type: String) {
        saveType = type
        saveDataToParamList()
        showLoading(getString(R.string.loading))
        when (intentType) {
            "edit" -> {
                mParam = MakePedigreeBookParam(id = mId, pgImg = imagePath, list = mParmaList)
                mViewModel.editBloodBook(mParam)
            }

            "blood" -> {
                mParam = MakePedigreeBookParam(id = bloodId, pgImg = imagePath, list = mParmaList)
                if (bloodId == 0) {
                    mViewModel.addBloodBook(mParam)
                } else {
                    mViewModel.editBloodBook(mParam)
                }
            }

            else -> {
                mParam = MakePedigreeBookParam(id = 0, pgImg = imagePath, list = mParmaList)
                mViewModel.addBloodBook(mParam)
            }
        }
    }

    private fun initListener() {
        //监听系统返回键
        this.onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                whetherToSaveData()
            }
        })

        mAdapter1.addChildClickViewIds(R.id.llParentLayout)
        mAdapter1.setOnItemChildClickListener { _, view, position ->
            if (getCheckChiefInfo()) {
                showMessage(getString(R.string.please_add_chief_pigeon))
                return@setOnItemChildClickListener
            }
            val title = when (position) {
                0 -> getString(R.string.father)
                1 -> getString(R.string.Chief_pigeon)
                else -> getString(R.string.mather)
            }
            when (view.id) {
                R.id.llParentLayout -> {
                    val ringId = mAdapter1.data[position].ringId.trim()
                    if (intentType == "blood" && position == 1) {
                        showEditBottomDialog(position, mAdapter1.data, title, "adapter1")
                        return@setOnItemChildClickListener
                    }
                    if (ringId.isNotEmpty()) {
                        showEditBottomDialog(position, mAdapter1.data, title, "adapter1")
                    } else {
                        showAddBottomDialog(position, title, "adapter1")
                    }
                }
            }
        }

        mAdapter2.addChildClickViewIds(R.id.llParentLayout)
        mAdapter2.setOnItemChildClickListener { _, view, position ->
            val title = when (position) {
                0 -> getString(R.string.grandfather)
                1 -> getString(R.string.grandmother)//"祖母"
                2 -> getString(R.string.grandfather_w)//"外祖父"
                else -> getString(R.string.grandmother_w)//"外祖母"
            }
            when (view.id) {
                R.id.llParentLayout -> {
                    val ringId = mAdapter2.data[position].ringId.trim()
                    if (ringId.isNotEmpty()) {
                        showEditBottomDialog(position, mAdapter2.data, title, "adapter2")
                    } else {
                        showAddBottomDialog(position, title, "adapter2")
                    }
                }
            }
        }

        mAdapter3.addChildClickViewIds(R.id.llParentLayout)
        mAdapter3.setOnItemChildClickListener { _, view, position ->
            val title = when (position) {
                0 -> getString(R.string.great_grandfather)//"曾祖父"
                1 -> getString(R.string.great_grandmother)//"曾祖母"
                2 -> getString(R.string.great_grandfather_w)//"曾外祖父"
                3 -> getString(R.string.great_grandmother_w_s)//"曾外祖母"
                4 -> getString(R.string.great_Grandfather_Yee)//"外曾祖父"
                5 -> getString(R.string.great_Grandmother_Yee)//"外曾祖母"
                6 -> getString(R.string.great_Grandfather_Yee_w)//"外曾外祖父"
                else -> getString(R.string.great_Grandmother_Yee_w)//"外曾外祖母"
            }
            when (view.id) {
                R.id.llParentLayout -> {
                    val ringId = mAdapter3.data[position].ringId.trim()
                    if (ringId.isNotEmpty()) {
                        showEditBottomDialog(position, mAdapter3.data, title, "adapter3")
                    } else {
                        showAddBottomDialog(position, title, "adapter3")
                    }
                }
            }
        }

        mAdapter4.addChildClickViewIds(R.id.llParentLayout)
        mAdapter4.setOnItemChildClickListener { _, view, position ->
            val title: String
            when (position) {
                0 -> title = getString(R.string.great_great_grandfather)
                1 -> title = getString(R.string.great_great_grandmother)//"高祖母"
                2 -> title = getString(R.string.great_great_grandfather_w)//"高外祖父"
                3 -> title = getString(R.string.great_great_grandmother_w)//"高外祖母"
                4 -> title = getString(R.string.Great_great_grandfather)//"曾外曾祖父"
                5 -> title = getString(R.string.Great_great_grandmother)//"曾外曾祖母"
                6 -> title = getString(R.string.Great_great_grandfather_w)//"曾外曾外祖父"
                7 -> title = getString(R.string.Great_great_grandmother_w)//"曾外曾外祖母"
                8 -> title = getString(R.string.waigaozufu)//"外高祖父"
                9 -> title = getString(R.string.waigaozumu)//"外高祖母"
                10 -> title = getString(R.string.waigaowaizufu)//"外高外祖父"
                11 -> title = getString(R.string.waigaowaizumu)//"外高外祖母"
                12 -> title = getString(R.string.waizengwaizengzufu)//"外曾外曾祖父"
                13 -> title = getString(R.string.waizengwaizengzumu)//"外曾外曾祖母"
                14 -> title = getString(R.string.waizengwaizengwaizufu)//"外曾外曾外祖父"
                else -> title = getString(R.string.waizengwaizengwaizumu)//"外曾外曾外祖母"
            }
            when (view.id) {
                R.id.llParentLayout -> {
                    val ringId = mAdapter4.data[position].ringId.trim()
                    if (ringId.isNotEmpty()) {
                        showEditBottomDialog(position, mAdapter4.data, title, "adapter4")
                    } else {
                        showAddBottomDialog(position, title, "adapter4")
                    }
                }
            }
        }
    }

    private fun getCheckChiefInfo(): Boolean {
        if (mAdapter1.data.isNotEmpty()) {
            if (mAdapter1.data[1].ringId.isEmpty()) {
                return false
            }
            return false
        }
        return true
    }

    @SuppressLint("MissingInflatedId")
    private fun showEditBottomDialog(position: Int, dataList: MutableList<EditBloodInfoBean>, title: String, adapter: String) {
        val dialog = BottomSheetDialog(this, R.style.BottomSheetDialog)
        val dialogView: View = LayoutInflater.from(this).inflate(R.layout.bottom_edit_dialog, null)
        val ivClose = dialogView.findViewById<AppCompatImageView>(R.id.ivClose)
        val tvClean = dialogView.findViewById<AppCompatTextView>(R.id.tvClean)
        val tvEditInfo = dialogView.findViewById<AppCompatTextView>(R.id.tvEditInfo)
        val tvTitle = dialogView.findViewById<AppCompatTextView>(R.id.tvTitle)
        val tvRingId = dialogView.findViewById<AppCompatTextView>(R.id.tvRingId)
        val ivSex = dialogView.findViewById<AppCompatImageView>(R.id.ivSex)
        val tvNickName = dialogView.findViewById<AppCompatTextView>(R.id.tvNickName)
        val tvFeatherColor = dialogView.findViewById<AppCompatTextView>(R.id.tvFeatherColor)
        val tvEye = dialogView.findViewById<AppCompatTextView>(R.id.tvEye)
        val tvDescribe = dialogView.findViewById<AppCompatTextView>(R.id.tvDescribe)
        notEditable = dataList[position].publicIs
        if (notEditable == 2) tvEditInfo.isGone = true
        if (adapter == "adapter1" && intentType == "blood" && position == 1) {
            tvNickName.isInvisible = false
            tvFeatherColor.isInvisible = false
            tvEye.isInvisible = false
            tvDescribe.isInvisible = false
            tvClean.isVisible = false
            tvEditInfo.text = getString(R.string.sure)
        } else {
            tvNickName.isInvisible = true
            tvFeatherColor.isInvisible = true
            tvEye.isVisible = false
            tvDescribe.isVisible = false
            tvClean.isVisible = true
            tvEditInfo.text = getString(R.string.copyreader)
        }
        tvRingId.text = dataList[position].ringId
        tvTitle.text = dataList[position].title
        tvNickName.text = dataList[position].name
        tvFeatherColor.text = dataList[position].featherColor
        tvEye.text = dataList[position].eye
        tvDescribe.text = dataList[position].gradesDesc
        val sexIcon = when (dataList[position].sex) {
            "雌" -> R.mipmap.icon_female
            "雄" -> R.mipmap.icon_male
            else -> R.mipmap.icon_sex_unknow
        }
        ivSex.setBackgroundResource(sexIcon)
        tvEditInfo.setOnClickListener {
            if (adapter == "adapter1" && intentType == "blood" && position == 1) {
                dialog.dismiss()
                return@setOnClickListener
            }
            val intentData: EditBloodInfoBean = when (adapter) {
                "adapter1" -> mAdapter1.data[position]
                "adapter2" -> mAdapter2.data[position]
                "adapter3" -> mAdapter3.data[position]
                else -> mAdapter4.data[position]
            }
            LogUtils.debugInfo(intentData.country)
            startActivityForResult<ActivityAddElder>("title" to title, "type" to 2, "data" to intentData) {
                if (it != null) {
                    val data = it.getParcelableExtra<EditBloodInfoBean>("data")
                    if (data != null) {
                        when (adapter) {
                            "adapter1" -> mAdapter1.updateItem(position, data)
                            "adapter2" -> mAdapter2.updateItem(position, data)
                            "adapter3" -> mAdapter3.updateItem(position, data)
                            else -> mAdapter4.updateItem(position, data)
                        }
                    }
                }
            }
            dialog.dismiss()
        }
        tvClean.setOnClickListener {//清空
            when (adapter) {
                "adapter1" -> mAdapter1.clearItem(position)
                "adapter2" -> mAdapter2.clearItem(position)
                "adapter3" -> mAdapter3.clearItem(position)
                else -> mAdapter4.clearItem(position)
            }
            dialog.dismiss()
        }
        ivClose.setOnClickListener {
            dialog.dismiss()
        }
        dialog.setContentView(dialogView)
        dialog.show()
    }

    @SuppressLint("MissingInflatedId")
    private fun showAddBottomDialog(position: Int, titleInfo: String, adapter: String) {
        val dialog = BottomSheetDialog(this, R.style.BottomSheetDialog)
        val dialogView: View = LayoutInflater.from(this).inflate(R.layout.bottom_add_dialog, null)
        val tvCancelAdd = dialogView.findViewById<AppCompatTextView>(R.id.tvCancelAdd)
        val tvToLeadIntoPigeonInfo = dialogView.findViewById<AppCompatTextView>(R.id.tvToLeadIntoPigeonInfo)
        val tvToLeadIntoPigeonInfoPublic = dialogView.findViewById<AppCompatTextView>(R.id.tvToLeadIntoPigeonInfoPublic)
        val tvAddPigeonInfo = dialogView.findViewById<AppCompatTextView>(R.id.tvAddPigeonInfo)

        tvToLeadIntoPigeonInfo.setOnClickListener {
            startActivityForResult<ActivityImportedBreedingPigeon>("title" to titleInfo) {
                if (it != null) {
                    notEditable = it.getIntExtra("edit", 0)
                    val data = it.getParcelableExtra<EditBloodInfoBean>("data")
                    if (data != null) {
                        when (adapter) {
                            "adapter1" -> mAdapter1.updateItem(position, data)
                            "adapter2" -> mAdapter2.updateItem(position, data)
                            "adapter3" -> mAdapter3.updateItem(position, data)
                            else -> mAdapter4.updateItem(position, data)
                        }
                    }
                }
            }
            dialog.dismiss()
        }

        tvToLeadIntoPigeonInfoPublic.setOnClickListener {
            startActivityForResult<ActivityImportedBreedingPigeonPublic>("title" to titleInfo) {
                if (it != null) {
                    notEditable = it.getIntExtra("edit", 0)
                    val data = it.getParcelableExtra<EditBloodInfoBean>("data")
                    if (data != null) {
                        when (adapter) {
                            "adapter1" -> mAdapter1.updateItem(position, data)
                            "adapter2" -> mAdapter2.updateItem(position, data)
                            "adapter3" -> mAdapter3.updateItem(position, data)
                            else -> mAdapter4.updateItem(position, data)
                        }
                    }
                }
            }
            dialog.dismiss()
        }

        tvAddPigeonInfo.setOnClickListener {
            startActivityForResult<ActivityAddElder>("title" to titleInfo, "type" to 1) {
                if (it != null) {
                    notEditable = it.getIntExtra("edit", 0)
                    val data = it.getParcelableExtra<EditBloodInfoBean>("data")
                    if (data != null) {
                        when (adapter) {
                            "adapter1" -> mAdapter1.updateItem(position, data)
                            "adapter2" -> mAdapter2.updateItem(position, data)
                            "adapter3" -> mAdapter3.updateItem(position, data)
                            else -> mAdapter4.updateItem(position, data)
                        }
                    }
                }
            }
            dialog.dismiss()
        }
        tvCancelAdd.setOnClickListener {
            dialog.dismiss()
        }
        dialog.setContentView(dialogView)
        dialog.show()
    }

    private fun selectCoverPic() {
        val selectionModel: PictureSelectionModel = PictureSelector.create(this)
            .openGallery(SelectMimeType.ofImage())
            .setSelectorUIStyle(PictureSelectorStyle())
            .setImageEngine(GlideEngine.createGlideEngine())
            .setSelectionMode(SelectModeConfig.SINGLE)
            .setCompressEngine(PictureUtil.getCompressFileEngine())
            .setPermissionDescriptionListener(PictureUtil.getPermissionDescriptionListener())
            .setPreviewInterceptListener(PictureUtil.getPreviewInterceptListener())
            .setPermissionDeniedListener(PictureUtil.getPermissionDeniedListener())
            .isLoopAutoVideoPlay(false)
            .isPageSyncAlbumCount(true)
            .setLanguage(LanguageConfig.SYSTEM_LANGUAGE)
            .setQuerySortOrder("")
            .isDisplayCamera(true)
            .isOpenClickSound(true)
            .isPreviewImage(false)
            .isMaxSelectEnabledMask(false)
            .isDirectReturnSingle(true)
            .setMaxSelectNum(1)
        selectionModel.forResult(object : OnResultCallbackListener<LocalMedia> {
            override fun onResult(result: ArrayList<LocalMedia>?) {
                pathUrl = result!![0].realPath
                /** 把单张图片压缩到1024k以内 同时像素不超过1200（宽、高都不大于1200） */
                EasyImgCompress.withSinglePic(this@ActivityMakeBlood, pathUrl)
                    .maxPx(1200)
                    .maxSize(1024)
                    .enableLog(true)
                    .setOnCompressSinglePicListener(object : OnCompressSinglePicListener {
                        override fun onStart() {}

                        override fun onSuccess(file: File) {
                            mViewModel.uploadGenealogyBookImg(file)
                        }

                        override fun onError(error: String) {}
                    }).start()
            }

            override fun onCancel() {}
        })
    }
}